<?php
namespace Inationsoft\Koma;

use Inationsoft\NS;
use Inationsoft\NS\Core\Controller;
use Inationsoft\NS\Core\DatabaseSessionHandler;
use Inationsoft\NS\Core\Model;
use Inationsoft\NS\UI\StyleManager;
use Inationsoft\NS\UI\ScriptManager;
use Inationsoft\NS\UI\Widget;
use Inationsoft\NS\Core\Config;

define('_NAMESPACE_', '\Inationsoft\Koma');
define('KOMA_APP_FORCE_ONLINE', true);

class Auth extends Controller {	
	function index() {
		if(User::getInstance()->isUser()) $this->redirect(Config::getInstance()->Koma->LoginRedirect);

		if(is_file(Config::getInstance()->Koma->ThemePath . '/admin/theme.php')) {
			Config::getInstance()->Koma->ThemePath .= '/admin';
			Config::getInstance()->Koma->ThemePublicURL .= '/admin';
		}

		Config::getInstance()->Koma->ThemeMainTemplate = 'theme-auth.php';
		Config::getInstance()->Koma->Subtitle = 'Sign In';

		StyleManager::getInstance()->addSource($this->Path . '/assets/css/auth.css');

		ScriptManager::getInstance()->addSource(NS_JQUERY_PATH);
		ScriptManager::getInstance()->addSource($this->Path . '/assets/js/auth.js');

		$this->View->File = 'auth/form.php';
		$this->View->assign(array(
			'form_title' => Config::getInstance()->Koma->Title,
			'username_caption' => 'Username' . new Widget\Hidden('username-placeholder', ('Username')),
			'password_caption' => 'Password',
		));
	}
	
	function login($async = false) {
		$m = Model::getInstance('SystemModel');

		if($user_data = $m->getUser($this->Request->Post->value('username'), $this->Request->Post->value('password'))) {
			if(!$user_data['Status']) $this->redirect(Config::getInstance()->Koma->LoginRedirect,  sprintf('Account is not yet activated, please activate the account first'));
			unset($user_data['Status']);

			$_SESSION['Koma']['Token'] = md5($user_data['UID'] . Config::getInstance()->Application->Salt);
			//$_SESSION['Koma']['Token'] = $user_data['Token'];
			$user_data['Token'] = md5($_SESSION['Koma']['Token'] . Config::getInstance()->Application->Salt);

			$user = new User($user_data);
			/*
			foreach($user_data as $key => $value) {
				$user->{$key} = $value;
			}
			*/
			$user->lock();
			
			/*
			$user->UID = $user_data['uid'];
			$user->RID = $user_data['rid'];
			$user->Username = $user_data['username'];
			$user->Role = $user_data['role'];
			$user->FullName = $user_data['fullname'];
			$user->Token = md5($user_data['token'] . Config::getInstance()->Application->Salt);
			$user->lock();
			*/

			$_SESSION['Koma']['User'] = serialize($user);
			
			DatabaseSessionHandler::getInstance()->regenerateID();
			//session_regenerate_id();

			if(!$async) $this->redirect(Config::getInstance()->Koma->LoginRedirect,  sprintf('Thank you for logging in, %s', $user_data['FullName']));
			else die(json_encode(array('auth' => true, 'redirect' => NS_BASE_URL . '/' . Config::getInstance()->Koma->LoginRedirect)));
		}

		if(!$async) $this->redirect('system/auth', 'You have have incorrect username or password!');
		else die(json_encode(array('auth' => false)));
	}

	function logout($token = '') {
		$user = User::getInstance();
		if($token == $user->Token && $token == md5($_SESSION['Koma']['Token'] . Config::getInstance()->Application->Salt)) {
			$_SESSION['Koma'] = array();
			session_regenerate_id();
		}

		$this->redirect(Config::getInstance()->Koma->LogoutRedirect);
	}
}
?>